package com.lanou.musicproject.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.musicproject.R;


import com.lanou.musicproject.activity.DownloadActivity;
import com.lanou.musicproject.activity.DownloadSeekActivity;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.RotateInfo;
import com.lanou.musicproject.bean.Song;
import com.lanou.musicproject.suspendservice.FxService;
import com.lanou.musicproject.utils.MusicPlayer;
import com.lanou.musicproject.utils.MyConfig;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;


/**
 * Created by dllo on 15/10/22.
 */
public class SongListViewAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private List<Song.DataEntity> data;
    private Song.DataEntity entity;
    private String show_link;
    private MusicPlayer musicPlayer;
    private boolean isStart = true;
    private ViewHodler songHolder;

    public SongListViewAdapter(Context c, List<Song.DataEntity> s) {
        mContext = c;
        data = s;
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        songHolder = null;
        if (convertView == null) {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.songfragment_item, null);
            songHolder = new ViewHodler();
            songHolder.song_listview_menu = (ImageView) convertView.findViewById(R.id.song_listview_menu);
            songHolder.songcontent_songer = (TextView) convertView.findViewById(R.id.songcontent_songer);
            songHolder.songcontent_title = (TextView) convertView.findViewById(R.id.songcontent_title);
            songHolder.song_listview_play = (ImageView) convertView.findViewById(R.id.song_listview_play);
            songHolder.item_linear = (LinearLayout) convertView.findViewById(R.id.item_linear);

            convertView.setTag(songHolder);

        } else {
            songHolder = (ViewHodler) convertView.getTag();
        }
        entity = (Song.DataEntity) getItem(position);
        if (entity != null) {
            songHolder.songcontent_songer.setText(entity.getSingerName());
            songHolder.songcontent_title.setText(entity.getName());
            songHolder.song_listview_menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    {
                        switch (v.getId()) {
                            case R.id.song_listview_menu:
                                PopupMenu pm = new PopupMenu(mContext, v);
                                pm.inflate(R.menu.menu_popup);
                                pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                                    @Override
                                    public boolean onMenuItemClick(MenuItem item) {
                                        switch (item.getItemId()) {
                                            case R.id.rank_share://分享
                                                share();//集成分享化组件
                                                break;
                                            case R.id.rank_collect://收藏
                                                break;
                                            case R.id.rank_download://下载

                                                Intent intent = new Intent(mContext, DownloadSeekActivity.class);

                                                if (data.get(position).getAuditionList().get(0).getUrl() != null) {
                                                    show_link = data.get(position).getAuditionList().get(0).getUrl();

                                                    String fileName =data.get(position).getName();
                                                    MyConfig.putSharePre(mContext,"filename","filename",fileName);

                                                    intent.putExtra("show_link", show_link);
                                                    MyConfig.putSharePre(mContext, "link", "url", show_link);
                                                    Log.i("*************/////", show_link);
                                                    mContext.startActivity(intent);
                                                } else {
                                                    Toast.makeText(mContext, "暂无此资源", Toast.LENGTH_LONG).show();
                                                }
                                                break;
                                        }
                                        return false;
                                    }

                                });
                                pm.show();
                                break;
                        }
                    }

                }
            });
        }

        songHolder.song_listview_play.setOnClickListener(this);
        songHolder.item_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MyMusicList> lists = new ArrayList<>();
                for ( Song.DataEntity entity : data) {
                    MyMusicList musicList = new MyMusicList();
                    musicList.setName(entity.getName());
                    musicList.setSinger(entity.getSingerName());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.mini_play(lists, position);

                FxService.mini_play(lists, position);
                MusicPlayActivity.playIv(lists, position);
            }
        });
        return convertView;
    }

    private void share() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.song_listview_play:

                musicPlayer = new MusicPlayer();
                //判断播放状态

                //第一个判断 正在播放 && 按钮是开始,点击暂停,按钮切换成暂停
                if (musicPlayer.isPlaying() && (isStart = true)) {
                    //如果播放器正在运行
                    musicPlayer.stop();
                    isStart = false;
                    //songHolder.song_listview_play.setBackgroundResource(R.mipmap.pause32);
                }
                //第二个判断 按钮是开始&&播放器空闲
                else if ((!musicPlayer.isPlaying()) && (isStart = true)) {

                    String download = MyConfig.getSharePreStr(mContext, "link", "url");


                    musicPlayer.playUrl(download);
                    isStart = false;
                    //songHolder.song_listview_play.setBackgroundResource(R.mipmap.pause32);
                }
                //按钮是暂停状态 播放器空闲 再点击继续播放
                else {
                            /*
                            *
                            *进度条记录位置之后直接seekto?
                            *
                            * */
                    musicPlayer.start();
                    isStart = true;
                    //songHolder.song_listview_play.setBackgroundResource(R.mipmap.play_32px);
                }


                break;
        }
    }

    private class ViewHodler {
        private TextView songcontent_title, songcontent_songer;
        private ImageView song_listview_menu, song_listview_play;
        private LinearLayout item_linear;

    }
}
